const crypto = require("crypto");
const jwt = require("jsonwebtoken");

module.exports = {
    /**
     * 把明文密码加密成密文
     * @param {string} password 明文密码
     */
    passwordEncoder(password) {
        return crypto
            .createHmac("sha256", this.app.config.keys)
            .update(password)
            .digest("hex");
    },
    genToken(payload) {
        return jwt.sign(payload, this.app.config.keys, { expiresIn: "1h" });
    },
    checkToken(token) {
        return jwt.verify(token, this.app.config.keys);
    },
};
